<?php
namespace app\common\model;
use think\Db;
class Dog extends \think\Model {

    protected $table = 'up_dog';
    public static $dog_seek_status = array(
        '1'=>'寻找中',
        '2'=>'已找回',
        '3'=>'暂停寻找',
    );
    public static $dog_status = array(
        '1'=>'待救助',
        '2'=>'已救助',
        '3'=>'失踪',
    );
    public static $dog_adopt_status = array(
        '1'=>'取消领养',
        '2'=>'待领养',
        '3'=>'完成领养',
        '4'=>'重新领养',
    );
    public static $dog_total = array(
        '1'=>'需要',
        '2'=>'不需',
    );
    public static $dog_adopt_environment = array(
        '1'=>'不接受合租',
        '2'=>'需要郊区',
        '3'=>'无要求',
    );
    public static $dog_adopt_sterilization = array(
        '1'=>'未绝育',
        '2'=>'已绝育',
        '3'=>'无要求',
    );
    //用户是否认证
    public static $user_attestation = array(
        '1'=>'未认证',
        '2'=>'已认证',
        '3'=>'认证中',
        '4'=>'认证失败'
    );
    //狗狗状况
    public static $udf_condition = array(
        '1' => '待救助流浪狗',
        '2' => '已救助流浪狗',
        '3' => '家养宠物',
        '4' => '不详'
    );
    //用户性别
    public static $user_gender = array(
        '1' => '男',
        '2' => '女'
    );
    //救助状态
    public static $dog_help_status = array(
        '1'  =>  '待帮助',
        '2'  =>  '已解决',
        '3'  =>  '暂停帮助',
        '4'  =>  '已过期'
    );
    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询领养柱状图数据
     */
//    public function managementDog($search,$type){
//        $model = self::instance();
//        $where = '';
//        if($search[0]!=''){
//            $where['u.area_sheng'] = array('=',$search[0]);
//        }
//        if($search[1]!=''){
//            $where['u.area_shi'] = array('=',$search[1]);
//        }
//        if($search[2]!=''){
//            $where['u.area_qu'] = array('=',$search[2]);
//        }
//        if($search[3]==''&&$search[4]==''){
//            $time = date('Y-m-d');
//            $start = date('Y-m-d',strtotime("-15 day"));
//            $where['u.dog_time'] = array('between',array($start,$time));
//        } elseif($search[3] > 0 && $search[4] > 0){
//            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
//        }else if($search[3] > 0){
//            $where['u.dog_time'] = array('EGT',$search[3]);
//        }else if($search[4]> 0){
//            $where['u.dog_time'] = array('ELT',$search[4]);
//        }else{
//            $where['u.dog_time'] = array('EGT',0);
//        }
//        $where['u.dog_type'] = 3;
//        $branch = $model->alias('u')
//            ->group('u.'.$type)
//            ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
//            ->join('up_area ua2','ua2.district_id = u.area_shi','left')
//            ->join('up_area ua3','ua3.district_id = u.area_qu','left')
//            ->where($where)
//            ->field('count(dog_id) as value,'.$type)->select();
//        //总的数据
//        return $branch;
//    }

    /**
     * @param   $pageSize  分页条数
     * @param   $search    查询条件
     * @return  $list      查询到的数据
     * 统计流浪狗信息
     */
    public function strayDogInfo($pageSize, $search){
        $model = self::instance();
        //搜索条件判断
        $where = '';
        if($search[0] != ''){
            $where['a.dog_status'] = ['=', $search[0]];
        }
        if($search[1] != ''){
            $where['a.area_sheng'] = ['=', $search[1]];
        }
        if($search[2] != ''){
            $where['a.area_shi'] = ['=', $search[2]];
        }
        if($search[3] != ''){
            $where['a.area_qu'] = ['=', $search[3]];
        }
        if($search[4] != '' && $search[5] != ''){
            $where['a.dog_release_time'] = array('between', array($search[4], $search[5]));
        }else if($search[4] != ''){
            $where['a.dog_release_time'] = array('EGT', $search[4]);
        }else if($search[5] != ''){
            $where['a.dog_release_time'] = array('ELT', $search[5]);
        }else{
            $where['a.dog_release_time'] = array('EGT', 0);
        }
        $where['a.dog_type'] = 1;
        $list = $model->alias('a')
            ->join('up_u_user uuu', 'uuu.user_id = a.user_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = a.area_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = a.area_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = a.area_qu', 'left')
            ->where($where)
            ->order('a.dog_id desc')
            ->field('a.dog_click_num,a.dog_id, a.dog_release_time, ua1.district as province, ua2.district as city, 
            ua3.district as district, a.dog_num, a.udsc_id, a.udsc_id_information, 
            uuu.user_username, uuu.user_phone, uuu.user_attestation, uuu.user_gender, uuu.user_card')
            ->paginate($pageSize);
        foreach($list as $key => &$val){
            $udsc_id['udsc_id'] =array('IN', trim($val['udsc_id'], ','));
            $udsc_name = Db::name('dog_strat_characteristic')->where($udsc_id)->column('udsc_name');//获取狗的特征
            if($udsc_name){
                if($val['udsc_id_information'] != ''){
                    $val['udsc_id_information'] = implode(',', $udsc_name).','.$val['udsc_id_information'];
                }else{
                    $val['udsc_id_information'] = rtrim(implode(',', $udsc_name), ',');
                }
            }
            //留言数量
            $whereMap['m_dog_id'] = $val['dog_id'];
            $count = Db::name('message')->where($whereMap)->count();
            $val['total'] = $count;
        }
        return $list;
    }
    /**
     * @param   $pageSize  分页条数
     * @param   $search    查询条件
     * @return  $list      查询到的数据
     * 统计互助信息
     */
    public function mutualDog($pageSize, $search){
        $model = self::instance();
        $where = '';
        if($search[0] != ''){
            $where['a.dog_help_status'] = ['=', $search[0]];
        }
        if($search[1] != ''){
            $where['a.area_sheng'] = ['=', $search[1]];
        }
        if($search[2] != ''){
            $where['a.area_shi'] = ['=', $search[2]];
        }
        if($search[3] != ''){
            $where['a.area_qu'] = ['=', $search[3]];
        }
        if($search[4] != '' && $search[5] != ''){
            $where['a.dog_release_time'] = array('between', array($search[4], $search[5]));
        }else if($search[4] != ''){
            $where['a.dog_release_time'] = array('EGT', $search[4]);
        }else if($search[5] != ''){
            $where['a.dog_release_time'] = array('ELT', $search[5]);
        }else{
            $where['a.dog_release_time'] = array('EGT', 0);
        }
        $where['a.dog_type'] = 2;
        $list = $model->alias('a')
            ->join('up_u_user uuu', 'uuu.user_id = a.user_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = a.area_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = a.area_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = a.area_qu', 'left')
            ->join('up_dog_family udf', 'udf.udf_id = a.udf_id', 'left')
            ->where($where)
            ->order('a.dog_id desc')
            ->field('a.udf_id,a.dog_click_num,a.dog_id, a.dog_release_time, a.dog_end_time, a.dog_help_style, 
            udf.udf_condition, uuu.user_username, uuu.user_attestation, uuu.user_phone')
            ->paginate($pageSize);
        foreach($list as $key => &$val){
            if($val['dog_end_time'] != ''){
                $val['dog_end_time'] = date('Y-m-d', strtotime($val['dog_end_time']));
            }
            $heo_id['heo_id'] = array('IN', trim($val['dog_help_style'], ','));
            $heo_name = Db::name('dog_heo')->where($heo_id)->column('heo_name');//获取互助类别
            if($heo_name){
                $val['dog_help_style'] = implode('、', $heo_name);
            }
        }
        return $list;
    }
    /****
     * @param $pageSize
     * @param $search
     * @return \think\Paginator
     * 领养管理首页列表
     */
    public function relData($pageSize,$search){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['a.dog_adopt_status'] = ['=',$search[0]];
        }
        if($search[1]!=''){
            $where['a.area_sheng'] =['=',$search[1]];
        }
        if($search[2]!=''){
            $where['a.area_shi'] =['=',$search[2]];
        }
        if($search[3]!=''){
            $where['a.area_qu'] = ['=',$search[3]];
        }
        if($search[4] != '' && $search[5] !=''){
            $where['a.dog_release_time'] = array('between',array($search[4],$search[5]));
        }else if($search[4] != ''){
            $where['a.dog_release_time'] = array('EGT',$search[4]);
        }else if($search[5]!=''){
            $where['a.dog_release_time'] = array('ELT',$search[5]);
        }else{
            $where['a.dog_release_time'] = array('EGT',0);
        }

        $where['a.dog_type']=3;
        $list = $model->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_area ua1','ua1.district_id = a.area_sheng','left')
            ->join('up_area ua2','ua2.district_id = a.area_shi','left')
            ->join('up_area ua3','ua3.district_id = a.area_qu','left')
            ->where($where)
            ->order('a.dog_update_time desc')
            ->field('a.*,uuu.user_username,ua1.district as province,ua2.district as city,ua3.district as district')
            ->paginate($pageSize);
        foreach ($list as $key=>&$vo){
            $whereMap['dog_id'] = $vo['dog_id'];
            $whereMap['proposer_status'] = 4;
            $proposer_status_total = Db::table('up_dog_proposer')->where($whereMap)->count();//重新领养统计
            $whereMapS['dog_id'] = $vo['dog_id'];
            $proposer_total = Db::table('up_dog_proposer')->where($whereMapS)->count();//领养统计
            $proposer_datetime = Db::table('up_dog_proposer')->where('dog_id', $vo['dog_id'])->order('proposer_datetime desc')->value('proposer_datetime');
            $vo['dog_update_time'] = $proposer_datetime;
            $vo['proposer_status_total'] = $proposer_status_total?$proposer_status_total:0;
            $vo['proposer_total'] = $proposer_total?$proposer_total:0;
            $whereU['user_id'] = $vo['user_id'];
            $user_total = Db::table('up_dog_family')->where($whereU)->count();//登记发布用户狗狗的数量
            $vo['user_total'] = $user_total?$user_total:0;
        }
        return $list;
    }

    /****
     * @param $pageSize
     * @param $search
     * @return \think\Paginator
     * 寻狗管理首页列表
     */
    public function findData($pageSize,$search){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['a.dog_seek_status'] = ['=',$search[0]];
        }
        if($search[1]!=''){
            $where['a.area_sheng'] =['=',$search[1]];
        }
        if($search[2]!=''){
            $where['a.area_shi'] =['=',$search[2]];
        }
        if($search[3]!=''){
            $where['a.area_qu'] = ['=',$search[3]];
        }
        if($search[4] != '' && $search[5] !=''){
            $where['a.dog_release_time'] = array('between',array($search[4],$search[5]));
        }else if($search[4] != ''){
            $where['a.dog_release_time'] = array('EGT',$search[4]);
        }else if($search[5]!=''){
            $where['a.dog_release_time'] = array('ELT',$search[5]);
        }else{
            $where['a.dog_release_time'] = array('EGT',0);
        }
        $where['a.dog_type']=4;
        $list = $model->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_area ua1','ua1.district_id = a.area_sheng','left')
            ->join('up_area ua2','ua2.district_id = a.area_shi','left')
            ->join('up_area ua3','ua3.district_id = a.area_qu','left')
            ->where($where)
            ->order('a.dog_update_time desc')
            ->field('a.*,uuu.user_username,ua1.district as province,ua2.district as city,ua3.district as district')
            ->paginate($pageSize);
        foreach ($list as $key=>&$vo){
            $heo_id['reason_id'] = array('IN', trim($vo['dog_seek_so'], ','));
            $reason_name = Db::table('up_dog_miss_reason')->where($heo_id)->column('reason_name');//获取狗失踪原因
            if(count($reason_name)>0){
                if($vo['dog_seek_so_qita']!=''){
                    $vo['reason'] = implode(',',$reason_name).','.$vo['dog_seek_so_qita'];
                }else{
                    $vo['reason'] = implode(',',$reason_name);
                }
            }else{
                $vo['reason'] = 'dog_seek_so_qita';
            }
        }
        return $list;
    }
    /***
     * @param $search
     * @return false|\PDOStatement|string|\think\Collection
     *
     */
    public function excelData($search,$type,$field){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['a.'.$field] = $search[0];
        }
        if($search[1]!=''){
            $where['a.area_sheng'] = ['=',$search[1]];
        }
        if($search[2]!=''){
            $where['a.area_shi'] = ['=',$search[2]];
        }
        if($search[3]!=''){
            $where['a.area_qu'] = ['=',$search[3]];
        }

        if($search[4] != '' && $search[5] != ''){
            $where['a.dog_release_time'] = array('between', array($search[4], $search[5]));
        }else if($search[4] != ''){
            $where['a.dog_release_time'] = array('EGT', $search[4]);
        }else if($search[5] != ''){
            $where['a.dog_release_time'] = array('ELT', $search[5]);
        }else{
            $where['a.dog_release_time'] = array('EGT', 0);
        }
        $where['a.dog_type'] = $type;
        $list = $model->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->join('up_area ua1','ua1.district_id = a.area_sheng','left')
            ->join('up_area ua2','ua2.district_id = a.area_shi','left')
            ->join('up_area ua3','ua3.district_id = a.area_qu','left')
            ->join('up_dog_family udfd','udfd.udf_id = a.udf_id','left')
            ->where($where)
            ->field('
            a.*,uuu.equipment,
            uuu.user_id,uuu.user_card,uuu.user_gender,uuu.user_attestation,uuu.user_id,uuu.user_username,
            ua1.district as province,ua2.district as city,ua3.district as district,
            udfd.udf_sex,udfd.udf_size,udfd.udf_age,udfd.udf_dog_vaccine,udfd.udf_dog_disability,udfd.udf_condition,
            udfd.udf_character, udfd.udf_character_other, udfd.udf_dog_outside, udfd.udf_dog_inside, udfd.udf_dog_sterilization')
            ->select();
        //留言数量
        foreach ($list as $key=>&$vo) {
            $whereMap['m_dog_id'] = $vo['dog_id'];
            $count = Db::table('up_message')->where($whereMap)->count();
            $vo['total'] = $count;
        }
        //当狗狗是流浪狗时，查询流浪狗特征
        if($type==1){
            foreach($list as $key => &$val){
                $udsc_id['udsc_id'] =array('IN', trim($val['udsc_id'], ','));
                $udsc_name = Db::name('dog_strat_characteristic')->where($udsc_id)->column('udsc_name');//获取狗的特征
                if($udsc_name){
                    $val['udsc_id_information'] = implode(',', $udsc_name).','.$val['udsc_id_information'];
                }
            }
        }
        //互助，查询互助类型，狗的性格
        if($type == 2){
            foreach($list as $key => &$val){
                //组装互助截至时间格式
                if($val['dog_end_time'] != ''){
                    $val['dog_end_time'] = date('Y-m-d', strtotime($val['dog_end_time']));
                }
                //互助类别
                $heo_id['heo_id'] = array('IN', trim($val['dog_help_style'], ','));
                $heo_name = Db::name('dog_heo')->where($heo_id)->column('heo_name');//获取互助类别

                if($heo_name){
                    if($val['dog_help_information'] != ''){
                        $val['dog_help_style'] = implode(',', $heo_name).','.$val['dog_help_information'];
                    }else{
                        $val['dog_help_style'] = implode(',', $heo_name);
                    }
                }else{
                    $val['dog_help_style'] = $val['dog_help_information'];
                }
                //狗的性格
                $uco_id['uco_id'] = array('IN', $val['udf_character']);
                $uco_character = Db::table('up_dog_character')->where($uco_id)->column('uco_character');
                if($uco_character){
                    if($val['udf_character_other'] != ''){
                        $val['udf_character'] = implode(',', $uco_character).','.$val['udf_character_other'];
                    }else{
                        $val['udf_character'] = implode(',', $uco_character);
                    }
                }else{
                    $val['udf_character'] = '';
                }
            }
        }
        if($type==4){//寻狗
            foreach ($list as $ke1=>&$vo1){
                $dog = Db::table('up_dog_seek_characteristic')->where('udskc_id',$vo1['dog_seek_characteristic'])->find();
                $whereC['udskca_id'] = array('IN',$dog['udskca_id']);
                $udskca_name = Db::table('up_dog_seek_characteristic_apparel')->where($whereC)->column('udskca_name');
                if(count($udskca_name)>0){
                    if($dog['udskca_id_other']!=''){
                        $vo1['name'] = implode(',',$udskca_name).','.$dog['udskca_id_other'];
                    }else{
                        $vo1['name'] = implode(',',$udskca_name);
                    }
                }else{
                    $vo1['name'] =$dog['udskca_id_other'];
                }

                $whereE['udsci_id'] = array('IN',$dog['udskci_id']);
                $udsci_name = Db::table('up_dog_seek_characteristic_other')->where($whereE)->column('udsci_name');
                if(count($udsci_name)>0){
                    if($dog['udskci_id_other']!=''){
                        $vo1['udsci_name'] = implode(',',$udsci_name).','.$dog['udskca_id_other'];
                    }else{
                        $vo1['udsci_name'] = implode(',',$udsci_name);
                    }
                }else{
                    $vo1['udsci_name'] =$dog['udskci_id_other'];
                }

                $heo_id['reason_id'] = array('IN', trim($vo1['dog_seek_so'], ','));
                $reason_name = Db::table('up_dog_miss_reason')->where($heo_id)->column('reason_name');//获取狗失踪原因
                if(count($reason_name)>0){
                    if($vo1['dog_seek_so_qita']!=''){
                        $vo1['reason'] = implode(',',$reason_name).','.$vo1['dog_seek_so_qita'];
                    }else{
                        $vo1['reason'] = implode(',',$reason_name);
                    }
                }else{
                    $vo1['reason'] = $vo1['dog_seek_so_qita'];
                }

                $vo1['udskc_sex'] =$dog['udskc_sex'];
                $vo1['udskc_habitus'] =$dog['udskc_habitus'];
                $vo1['udskc_hair'] =$dog['udskc_hair'];
                $vo1['udskc_tail'] =$dog['udskc_tail'];
            }
        }
        return $list;
    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询领养管理每个的数据
     */
    public function branch($search,$whereS,$type,$dog_type=3){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-1 month"));
            $where['u.dog_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.dog_time'] = array('EGT',$search[3]);
        }else if($search[4]> 0){
            $where['u.dog_time'] = array('ELT',$search[4]);
        }else{
            $where['u.dog_time'] = array('EGT',0);
        }
        $where['u.dog_type'] = $dog_type;
        $branch = $model->alias('u')
            ->group('u.'.$type)
            ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
            ->join('up_area ua2','ua2.district_id = u.area_shi','left')
            ->join('up_area ua3','ua3.district_id = u.area_qu','left')
            ->where($where)
            ->where($whereS)
            ->field('count(dog_id) as value,sum(u.dog_num) as num,'.$type)->select();
        //总的数据
        return $branch;
    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询领养管理条件数据总和
     */
    public function branchCount($search){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-1 month"));
            $where['u.dog_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.dog_time'] = array('EGT',$search[3]);
        }else if($search[4]> 0){
            $where['u.dog_time'] = array('ELT',$search[4]);
        }else{
            $where['u.dog_time'] = array('EGT',0);
        }
        $where['u.dog_type'] = 3;
        $branch = $model->alias('u')
                        ->where($where)
                        ->count();
        //总的数据
        return $branch;
    }
    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 寻狗的柱状图统计
     */
//    public function findBranch($search,$type){
//        $model = self::instance();
//        $where = '';
//        if($search[0]!=''){
//            $where['u.area_sheng'] = array('=',$search[0]);
//        }
//        if($search[1]!=''){
//            $where['u.area_shi'] = array('=',$search[1]);
//        }
//        if($search[2]!=''){
//            $where['u.area_qu'] = array('=',$search[2]);
//        }
//        if($search[3]==''&&$search[4]==''){
//            $time = date('Y-m-d');
//            $start = date('Y-m-d',strtotime("-15 day"));
//            $where['u.dog_time'] = array('between',array($start,$time));
//        } elseif($search[3] > 0 && $search[4] > 0){
//            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
//        }else if($search[3] > 0){
//            $where['u.dog_time'] = array('EGT',$search[3]);
//        }else if($search[4]> 0){
//            $where['u.dog_time'] = array('ELT',$search[4]);
//        }else{
//            $where['u.dog_time'] = array('EGT',0);
//        }
//        $where['u.dog_type'] = 4;
//        $branch = $model->alias('u')
//            ->group('u.'.$type)
//            ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
//            ->join('up_area ua2','ua2.district_id = u.area_shi','left')
//            ->join('up_area ua3','ua3.district_id = u.area_qu','left')
//            ->where($where)
//            ->field('count(dog_id) as value,'.$type)->select();
//        //总的数据
//        return $branch;
//    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 寻狗的饼状图统计
     */
    public function findPie($search,$whereH){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-1 month"));
            $where['u.dog_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.dog_time'] = array('EGT',$search[3]);
        }else if($search[4]> 0){
            $where['u.dog_time'] = array('ELT',$search[4]);
        }else{
            $where['u.dog_time'] = array('EGT',0);
        }
        $where['u.dog_type'] = 4;
        $branch = $model->alias('u')
            ->where($where)
            ->where($whereH)
            ->count();
        //总的数据
        return $branch;
    }
    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 寻狗的数据统计
     */
    public function totalDogNum($search,$type,$whereS){
        $model = self::instance();
        $where = '';
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        if($search[3]==''&&$search[4]==''){
            $time = date('Y-m-d');
            $start = date('Y-m-d',strtotime("-15 day"));
            $where['u.dog_time'] = array('between',array($start,$time));
        } elseif($search[3] > 0 && $search[4] > 0){
            $where['u.dog_time'] = array('between',array($search[3],$search[4]));
        }else if($search[3] > 0){
            $where['u.dog_time'] = array('EGT',$search[3]);
        }else if($search[4]> 0){
            $where['u.dog_time'] = array('ELT',$search[4]);
        }else{
            $where['u.dog_time'] = array('EGT',0);
        }
        $where['u.dog_type'] = $type;
        $branch = $model->alias('u')
            ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
            ->join('up_area ua2','ua2.district_id = u.area_shi','left')
            ->join('up_area ua3','ua3.district_id = u.area_qu','left')
            ->where($where)
            ->where($whereS)
            ->count();
        //总的数据
        return $branch;
    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询领养柱状图数据
     */
    public function columnDog($search,$whereMap){
        $where = '';
        $model = self::instance();
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        $where['u.dog_type'] = 3;
        $branch = $model->alias('u')
//            ->join('up_area ua1','ua1.district_id = u.area_sheng','left')
//            ->join('up_area ua2','ua2.district_id = u.area_shi','left')
//            ->join('up_area ua3','ua3.district_id = u.area_qu','left')
            ->where($where)
            ->where($whereMap)
            ->count();
        //总的数据
        return $branch;
    }

    /**
     * @param $search
     * @param $whereS
     * @return int|string
     * 查询寻狗柱状图数据
     */
    public function findColumnDog($search,$whereMap){
        $where = '';
        $model = self::instance();
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        $where['u.dog_type'] = 4;
        $branch = $model->alias('u')
            ->where($where)
            ->where($whereMap)
            ->count();
        //总的数据
        return $branch;
    }

    /**
     * @param $where
     * @return int|string
     * 统计我的狗窝数量
     */
    public function countNum($where){
        $model = self::instance();
        $count = $model->where($where)->count();
        return $count;
    }

    private static $instance;

    public static function instance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}